package com.itheima;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

//演示Collections集合工具类的使用
public class CollectionsDemo {
    public static void main(String[] args) {
        test0();
        test1();
    }

    private static void test0() {
        //给集合对象批量添加元素
        List<String> list = new ArrayList<>();
        Collections.addAll(list, "张三", "李四", "王五", "赵六", "田七");
        list.forEach(System.out::println);
        System.out.println("--------------------------------");
        //打乱集合的顺序
        Collections.shuffle(list);
        list.forEach(System.out::println);
    }

    public static void test1() {
        List<Integer> list = new ArrayList<>();
        Collections.addAll(list, 1,2,3,4,5,6,7,8);
        //按照数字正序排序
        Collections.sort(list);
        list.forEach(System.out::println);
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                //按照数字倒序排序
                return o2 - o1;
            }
        });
        System.out.println("--------------------------------");
        list.forEach(System.out::println);

        List<Student> list1 = new ArrayList<>();
        list1.add(new Student("张三", 24));
        list1.add(new Student("王五", 19));
        list1.add(new Student("赵六", 20));
        list1.add(new Student("田七", 16));
        //按照年龄升序排序
        Collections.sort(list1);
        System.out.println("--------------------------------");
        list1.forEach(System.out::println);

    }


}
